package cn.wolfcode.luowowo.website.controller;
import cn.wolfcode.luowowo.article.domain.Answer;
import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Question;
import cn.wolfcode.luowowo.article.query.AnswerQuery;
import cn.wolfcode.luowowo.article.query.QuestionQuery;
import cn.wolfcode.luowowo.article.service.IAnswerService;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IQuestionService;
import cn.wolfcode.luowowo.cache.service.IAnswerRedisService;
import cn.wolfcode.luowowo.common.exception.LogistException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.server.IUserInfoService;
import cn.wolfcode.luowowo.website.anno.UserParm;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("question")
public class QuestionController {
    @Reference
    private IQuestionService questionService;

    @Reference
    private IAnswerService answerService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IAnswerRedisService answerRedisService;


    @RequestMapping("")
    public String index(Model model, @ModelAttribute("qo") QuestionQuery qo) {
        if(qo==null){
            qo=new QuestionQuery();
        }
        qo.setPageSize(Integer.MAX_VALUE);
        PageInfo<Question> pageInfo = questionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "question/wenda";
    }

    @RequestMapping("input")
    public String input(Model model , @UserParm UserInfo userInfo) {
        if(userInfo==null){
            throw new LogistException("请登录");
        }
        List<Destination> dests = destinationService.list();
        model.addAttribute("dests",dests);
        return "question/public";
    }

    @RequestMapping("inputAnswer")
    public String inputAnswer(Model model , @UserParm UserInfo userInfo, @ModelAttribute("qo") AnswerQuery answerQuery) {
        if(userInfo==null){
            throw new LogistException("请登录");
        }
        return "question/input";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(String tag,String editorValue,Question question , @UserParm UserInfo userInfo) {
        if(userInfo==null){
            throw new LogistException("请登录");
        }
        question.setContent(editorValue);
        question.setAuthor(userInfo);
        System.err.println(tag);
        System.out.println(question);
        //question.setTag("");
        question.setTag(tag);
        System.err.println(question.getTagList());
        System.err.println(question.getTag());
        /*for (String s : question.getTagList()) {
            System.err.println(s);
            question.setTag(question.getTag()+s+",");
        }*/
        questionService.saveOrUpdate(question);
        return AjaxResult.SUCCESS;
    }


    @RequestMapping("saveAnswer")
    @ResponseBody
    public Object saveAnswer(Answer answer , @UserParm UserInfo userInfo) {
        if(userInfo==null){
            throw new LogistException("请登录");
        }
        answer.setAuthor(userInfo);
        answerService.saveOrUpdate(answer);
        questionService.addreplynum(answer.getQuestionId(),1);
        return AjaxResult.SUCCESS.addData(answer.getQuestionId());
    }


    @RequestMapping("detail")
    public String detail(Model model, @ModelAttribute("qo") AnswerQuery qo,@UserParm UserInfo userInfo) {
        model.addAttribute("userInfo",userInfo);
        Question question = questionService.selectByPrimaryKey(qo.getQuestionId());
        model.addAttribute("question",question);
        qo.setPageSize(Integer.MAX_VALUE);
        PageInfo<Answer> pageInfo = answerService.query(qo);
        for (Answer answer:pageInfo.getList()){
            System.out.println(answer);
        }
        Integer attention = questionService.queryAttention(userInfo.getId(),qo.getQuestionId());
        model.addAttribute("attention",0);
        if(attention >0){
            model.addAttribute("attention",1);
        }
        model.addAttribute("answers",pageInfo.getList());
        return "question/wendaDetail";
    }

    @RequestMapping("/attention")
    @ResponseBody
    public Object attention(Integer state,Long questionId,@UserParm UserInfo userInfo){
        if(userInfo==null){
            throw new LogistException("请登录");
        }
        questionService.setAttention(questionId,userInfo.getId(),state);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/answerThumbup")
    @ResponseBody
    public Object answerThumbup(Long answerId,@UserParm UserInfo userInfo){
        if(userInfo==null){
            AjaxResult result = new AjaxResult(false,"请登录");
            result.setCode(102);
            return  result;
        }
        boolean success = answerRedisService.answerThumbup(userInfo.getId(),answerId);
        if(success){
            answerService.addThumbup(answerId,1);
            Answer answer = answerService.selectByPrimaryKey(answerId);
            questionService.addthumbupnum(answer.getQuestionId(),1);
        }
        return new AjaxResult(success);
    }

    @RequestMapping("/replynumTop")
    public String replynumTop(Model model){
        List<HashMap<String, Object>> maps = answerService.replynumTop();
        for (HashMap<String, Object> map : maps) {
            if(map.containsKey("xx")){
                maps.remove(map);
            }
            System.err.println(map);
        }
        model.addAttribute("replynumTop",maps);
        return "question/replynumTop";
    }

    @RequestMapping("/thumbsupnumTop")
    public String thumbsupnumTop(Model model){
        List<HashMap<String, Object>> maps = answerService.thumbsupnumTop();
        for (HashMap<String, Object> map : maps) {
            if(map.containsKey("xx")){
                maps.remove(map);
            }
            System.err.println(map);
        }
        model.addAttribute("thumbsupnumTop",maps);
        return "question/thumbsupnumTop";
    }

    @RequestMapping("/addviewnum")
    @ResponseBody
    public String addviewnum(Long questionId){
        questionService.addviewnum(questionId,1);
        return "ok";
    }
}


